//feature: https://github.com/mrdoob/three.js/issues/167 no se admiten geometries crecientes
//			console.log("1",  Math.acos((new THREE.Vector3(1,0,0)).dot(new THREE.Vector3(1,0,1).normalize())));
//			console.log("2",  Math.acos((new THREE.Vector3(1,0,0)).dot(new THREE.Vector3(1,0,-1).normalize())));
//			console.log("3",  Math.acos((new THREE.Vector3(1,0,0)).dot(new THREE.Vector3(-1,0,1).normalize())));
//			console.log("4",  Math.acos((new THREE.Vector3(1,0,0)).dot(new THREE.Vector3(-1,0,-1).normalize())));


/////////////////////////CREAR MUNDO (con escena de Blender)

function crearMundo(scene){					
				
				
				//creo las flechas y las agrego a la escena
				var loader = new THREE.JSONLoader();
				loader.createModel(UTILS.flechaData, function( geometry ) { 
					var mat1 =  new THREE.MeshLambertMaterial({ color: '0x00ff00'});
					var mat2 =  new THREE.MeshLambertMaterial({ color: '0xff0000'});
					_meshFlechaInicio = new THREE.Mesh( geometry, mat1 );
					_meshFlechaDestino = new THREE.Mesh(geometry, mat2);
					_meshFlechaInicio.visible = false;
					_meshFlechaDestino.visible = false;
					scene.add(_meshFlechaInicio);
					scene.add(_meshFlechaDestino);
					mat1 = mat2 = null;
				});
				
				
				
				if(MOSTRAR_ESCENA){
				var loader = new THREE.SceneLoader();

				loader.load( "obj/escenauni.js", function(result){
					//console.log("result", result);
					var unMergedMeshes = [];
					for (var obj in result.objects){
						var malla = result.objects[obj]; 
						if (malla instanceof THREE.Mesh){
							//console.log("yyyyyyyyyyyyyyyyyyyes");
							
							//HACK: para pintar todos los autos de un color random
							if(malla.name.substring(4,0).toLowerCase() == "auto"){
								malla.geometry.materials[1] = new THREE.MeshLambertMaterial( { color: colores[count]} );
								console.log("entreeeeeeeeeeeeeeeeeeeeee", malla.name,colores[count]);
								count = (count + 1) % colores.length;
							}
							
						unMergedMeshes.push(malla);
							
						}
					}
					result.scene = null;
					result = null;
					//console.log("scene",scene);
					
					var mergedGeo = new THREE.Geometry();
					for (var i = 0, l = unMergedMeshes.length; i<l; i++){	
					//	console.log("entreeeeeeeeeeeeeeeees");
						
						
						//HACK: para randomizar los colores de los autos
						if(unMergedMeshes[i].name.substring(4,0).toLowerCase() == "auto"){
//								mergedMesh.geometry.materials[0] = MATERIAL_BUILDER.getMaterial("arrow_portal_externo");
							
							//var rand = "0x" + (Math.floor(Math.random()*16777215).toString(16));
							//console.log("rand", rand);
							//unMergedMeshes[i].geometry.materials[1] = new THREE.MeshLambertMaterial( { color: rand} );
							
							unMergedMeshes[i].geometry.materials[1] = colores[count];
							
							
//								unMergedMeshes[i].geometry.materials[1] = new THREE.MeshLambertMaterial( { color: colores[count]} );
							//console.log("ntreeeeeeeeeeeeeeeeeeeeee", malla.name,colores[count]);
							count = (count + 1) % colores.length;
						}
						
						merge(mergedGeo, unMergedMeshes[i]);
						
					}
					var mergedMesh = new THREE.Mesh(mergedGeo, MeshMat);
					
					unMergedMeshes = null;
					unMergedMeshes = [];
					
					mergedMesh.rotation.x = -Math.PI/2;
					scene.add(mergedMesh);
				//	console.log("merged", mergedMesh);
					mergedGeo = null;
					escenaCompleta = mergedMesh ;
					mergedMesh = null;
					
				} );

				}
//					//OBJETO POR OBJETO: PARA DEBUG
//					loader.load( "obj/escenauni.js", function(result){
//						//console.log("result", result);
//						var unMergedMeshes = [];
//						for (var obj in result.objects){
//							var malla = result.objects[obj]; 
//							if (malla instanceof THREE.Mesh){
//							unMergedMeshes.push(malla);
//														
//						}
//						}
//						result.scene = null;
//						result = null;
//						//console.log("scene",scene);
//						
//						
//						for (var i = 0, l = unMergedMeshes.length; i<l; i++){	
//						//	console.log("entreeeeeeeeeeeeeeeees");
//							var mergedGeo = new THREE.Geometry();
//							merge(mergedGeo, unMergedMeshes[i]);
//							var mergedMesh = new THREE.Mesh(mergedGeo, MeshMat);
//							mergedMesh.rotation.x = -Math.PI/2;
//							if(unMergedMeshes[i].name.substring(4,0).toLowerCase() == "auto"){
////								mergedMesh.geometry.materials[0] = MATERIAL_BUILDER.getMaterial("arrow_portal_externo"); 
//								mergedMesh.geometry.materials[0] = new THREE.MeshLambertMaterial( { color: colores[count]} );
//								//console.log("ntreeeeeeeeeeeeeeeeeeeeee", malla.name,colores[count]);
//								count = (count + 1) % colores.length;
//								
//								
//							}
//							
//							scene.add(mergedMesh);
//								console.log("merged", mergedMesh);
//						}
//						//var mergedMesh = new THREE.Mesh(mergedGeo, MeshMat);
//						
//						unMergedMeshes = null;
//						unMergedMeshes = [];
//						
//						
//						//scene.add(mergedMesh);
//					//	console.log("merged", mergedMesh);
//						mergedGeo = null;
//						mergedMesh = null;
//						
//					} );
//					
				
				
				//escena para el edificio de alem. TDOO esto esta harcodeado (habria que modularizarlo para los distintos edificios)
				var JSONloader =  new THREE.JSONLoader();
//					DEPRECATED: JSONLoader( parameters ) is now JSONLoader( url, callback, texturePath ).
//					JSONloader.load( { model: "obj/escena.js", 
//					  callback: function( geometry ) {	scene.add(new THREE.Mesh(geometry, new THREE.MeshFaceMaterial())); }
//					 });
				JSONloader.load(  "obj/escena_solo_planos.js" , 
					function( geometry ) {	
					escenaPlanos = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial());
					if (MOSTRAR_ESCENA) escenaPlanos.visible = false;
					scene.add(escenaPlanos);
					}
				);
//					JSONloader.load(  "obj/probando.js"/*PATH_ESCENA */, 
//							function( geometry ) {	
//						//escenaCompleta = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial());
//						//scene.add(escenaCompleta);
//					}
//					);
				
				
				if (MOSTRAR_TEXTO_3D){//var global
				
				var tamanio = 500;
				var prof = 150;
				var color = 0xff0000;
				var alem = 	getCenteredTextMesh({texto : "Av. Alem", targetObject : scene,  paramsTexto: {size : tamanio, height : prof, color: color}});
//					var alem = 	getTexto3D({texto : "Av. Alem", size : tamanio, height : prof});
				alem.position.x = 1563 ;//- alem.geometry.ancho();
				alem.position.y = -50;
				alem.position.z = 2770;
				
				
				var cordoba = 	getCenteredTextMesh({texto : "Cordoba", targetObject : scene,  paramsTexto: {size : tamanio, height : prof, color: color}});
//					var alem = 	getTexto3D({texto : "Av. Alem", size : tamanio, height : prof});
				cordoba.position.x = -3146 ;//- alem.geometry.ancho();
				cordoba.position.y = -50;
				cordoba.position.z = -3048;
					
				
				var doce = 	getCenteredTextMesh({texto : "12 de Octubre", targetObject : scene,  paramsTexto: {size : tamanio, height : prof, color: color}});
//					var alem = 	getTexto3D({texto : "Av. Alem", size : tamanio, height : prof});
				doce.position.x = 1563 ;//- alem.geometry.ancho();
				doce.position.y = -50;
				doce.position.z = -7675;
			
				
				var sanjuan = 	getCenteredTextMesh({texto : "San Juan", targetObject : scene,  paramsTexto: {size : tamanio, height : prof, color: color }});
//					var alem = 	getTexto3D({texto : "Av. Alem", size : 800, height : prof});
				sanjuan.position.x = 6016 ;//- alem.geometry.ancho();
				sanjuan.position.y = -50;
				sanjuan.position.z = -3048;
				
				
				alem.useQuaternion = true;	
				cordoba.useQuaternion = true;
				doce.useQuaternion = true;	
				sanjuan.useQuaternion = true;
				
				//agregamos los textos a un arreglo que se utiliza en animate() para orientarlos					
				nombreCalles.push(alem);
				nombreCalles.push(cordoba);
				nombreCalles.push(doce);
				nombreCalles.push(sanjuan);
				
				for (var i = 0, l = nombreCalles.length; i<l; i++){
					nombreCalles[i].rotation.x = -Math.PI/2;
					nombreCalles[ i ].quaternion = camera.quaternion;
				}
				cordoba.rotation.z = Math.PI/2;
				sanjuan.rotation.z = -Math.PI/2;
				
				
				}
				//Tridente y grilla
				if (false){
					//Tridente
					//scene.add(new Trident({length: 1200}));
					
					// Grid
					var geometry = new THREE.Geometry();
					geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( - 500, 0, 0 ) ) );
					geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( 500, 0, 0 ) ) );
						
					for ( var i = 0; i <= 20; i ++ ) {
						var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2 } ) );
						line.position.z = ( i * 50 ) - 500;
						scene.add( line );

						var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2 } ) );
						line.position.x = ( i * 50 ) - 500;
						line.rotation.y = 90 * Math.PI / 180;
						scene.add( line );
					}
				}
			}